package com.demo.List.ArrayList;

import org.omg.CORBA.PolicyError;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @Author :蕉绿
 * @Date : 2023/2/2 10:22
 * @Version : V1.0
 * @Description :
 */
public class MyArrayList {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    //打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    //在数组最后新增元素
    public void add(int val) {

        try {
            if(isFull()) {
                this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
            }
        } catch (NegativeArraySizeException e) {
            e.printStackTrace();
        }
        this.elem[usedSize] = val;
        usedSize++;
    }
    //在 pos 位置新增元素
    public void add(int pos,int data) {
        try {
            checkPos(pos);
        } catch (PosIndexNotLegalException e) {
            e.printStackTrace();
        }
        if(isFull()) {
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        int size = usedSize;
        for (int i = size; i > pos ; i--) {
            elem[size] = elem[size - 1];
            size--;
        }
        elem[pos] = data;
        usedSize++;
    }
    //判满
    private boolean isFull() {
        return usedSize == this.elem.length;
    }
    //检查下标是否合法
    private void checkPos(int pos) throws PosIndexNotLegalException {
        if(pos < 0 || pos >= usedSize) {
            throw new PosIndexNotLegalException("pos下标不合法!");
        }

    }

    //是否包含某个元素
    public boolean contains(int data) {
        for (int i = 0; i < usedSize; i++) {
            if(data == this.elem[i]) {
                return true;
            }
        }
        return false;
    }
    //查找某个元素对应的位置
    public int indexOf(int data) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == data) {
                return i;
            }
        }
        return -1;
    }
    //获取 pos 处的元素
    public int get(int pos) {
        try {
            checkPos(pos);
        } catch (PosIndexNotLegalException e) {
            e.printStackTrace();
        }
        return elem[pos];
    }

    //给 pos 位置设置新的值
    public void set(int pos,int value) {
        try {
            if(pos < 0 || pos > usedSize) {
                throw new PosIndexNotLegalException();
            }
            this.elem[pos] = value;
        } catch (PosIndexNotLegalException e) {
            e.printStackTrace();
        }
    }
    //删除第一次出现的key值
    public void remove(int key) {
        int index = -1;
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == key) {
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有你要删除的数字!");
            return;
        }
        for (int i = index; i < usedSize; i++) {
            elem[i] = elem[i + 1];
        }
        usedSize--;
    }

    //获取顺序表的长度
    public int size() {
        return usedSize;
    }
    //清空顺序表
    public void clear() {
        usedSize = 0;
    }
}
